import React from "react";
import {observer, inject} from 'mobx-react';
import * as mobx from 'mobx';
import {withRouter, Route} from 'react-router-dom';
import {Tools, Toast, Bubble, SearchBar} from "weaver-mobile";
import {RouteLayout, ListViewPage} from "weaver-mobile-page";

const getLabel = Tools.getLabel;
const ButtonGroup = RouteLayout.ButtonGroup;

@withRouter
@inject("odocRequestStore", "odocComsStore")
@observer
export default class SelectDirectionList extends React.Component {
    constructor(props) {
        super(props);

        this.onSubmit = this.onSubmit.bind(this);
        this.onSearch = this.onSearch.bind(this);
    }

    closePage() {
        const {odocRequestStore} = this.props;
        window.historyRoute.go(-1);
    }

    onSubmit() {
        const {location, odocRequestStore} = this.props;
        const {callback} = location;
        const {selectListStore} = odocRequestStore;
        let {selectedRowKeys} = selectListStore;
        selectedRowKeys = mobx.toJS(selectedRowKeys);

        //回调方法
        if (selectedRowKeys.length > 0) {
            this.closePage();
            callback && typeof callback == "function" && callback(selectedRowKeys[0]);
        } else {
            Bubble.error(getLabel(523436, "请选择常用去向"), 2);
        }
    }

    onSearch(directname) {
        const {odocRequestStore, odocComsStore} = this.props;
        let {requestParam = {}} = odocComsStore;
        requestParam = mobx.toJS(requestParam);
        delete requestParam.markInfo;

        odocRequestStore.resetListStore();
        odocRequestStore.getTableList({...requestParam, directname});
    }

    render() {
        const {odocRequestStore} = this.props;
        const {selectListStore} = odocRequestStore;
        let {selectedRowKeys} = selectListStore;
        selectedRowKeys = mobx.toJS(selectedRowKeys);
        const disabled = selectedRowKeys.length <= 0;

        const buttons = [
            {
                key: 'submit',
                text: getLabel(501196, "确定"),
                onClick: this.onSubmit,
                disabled,
                type: 'primary',
            }
        ];

        const flextStyles = [
            {
                style: {flex: 1}
            }
        ];

        return (
            <div key="odoc_select_list" className="wm-route-layout">
                <div className="odoc-request-select-list">
                    <SearchBar placeholder={getLabel(	523434, "请输入常用去向名称")} onSubmit={this.onSearch}/>
                    <ListViewPage.Normal
                        showCheck={true}
                        store={selectListStore}
                        rowKey="directionId"
                        multipleCheck={false}
                        heightSpace={47}
                    />
                    <ButtonGroup data={buttons} flexStyles={flextStyles}/>
                </div>
            </div>
        );
    }
}